import Vue from "vue"
import Router from "vue-router"
import store from "@/store"
import layout from "@/views/layout"
import NProgress from "nprogress"
import "nprogress/nprogress.css"
import {Vue2Storage} from "vue2-storage";
// Progress 进度条样式

/*eslint no-undef:0*/
const _import = require("./_import_" + process.env.NODE_ENV);
Vue.use(Router);
Vue.use(Vue2Storage, {
	prefix: "app_",
	driver: "session",
	ttl: 60 * 60 * 24 * 1000 // 24 часа
})

let router = new Router({
	routes: [
		{
			path: "/",
			redirect: "/index/updateRecord"
		},
		{
			path: "/login",
			name: "login",
			component: _import("/login"),
			meta: {
				noAuth: true
			}
		},
		{
			path: "/qywxAuth/:path?",
			name: "qywxAuth",
			component: _import("/qywxAuth"),
			props: true,
			meta: {
				title: "微信企业认证",
				noAuth: true
			}
		}
	]
})

router.beforeEach((to, from, next) => {
	NProgress.start()
	store.dispatch("initSysConfig").then(() => {
		/* 判断要访问的路由是否需要权限访问 */
		if (to.meta && to.meta.noAuth) {
			next()
		} else {
			store.dispatch('getDict' );
			/* 判断是否有登录信信息*/
			if (store.getters.hasSession) {
				//暂时处理hasSession有时返回false的bug
				if (Vue.$storage.get("token")) {
					/* 判断是否已读取用户信息 */
					if (store.getters.hasUserInfo) {
						next()
					} else {
						/* 获取用户信息 */
						store.dispatch("getUserInfo").then(res => {
							let menuList = res.data.menuList
							let menuRouters = []
							/*先取出模块，没有父id的就是模块菜单*/
							menuList.forEach((m, i) => {
								//console.log(i)
								if (m.parent_id === 0) {
									m.fullPath = "/" + m.path
									let module = {
										path: "/" + m.path,
										component: layout,
										meta: {id: m.id, title: m.title, fullPath: "/" + m.path},
										children: [
											{
												path: "",
												component: _import("/" + m.path + "/index"),
												meta: {
													menuHide: true,
													title: m.title
												}
											}
										]
									}
									menuRouters.push(module)
								}
							})

							function convertTree(routers) {
								routers.forEach(r => {
									menuList.forEach((m, i) => {
										if (m.parent_id && m.parent_id == r.meta.id) {
											if (!r.children) r.children = []
											m.fullPath = r.meta.fullPath + "/" + m.path
											let menu = {
												path:m.path,
												// component: () => import('@/views'+r.meta.fullPath+'/'+m.path),
												component: _import(r.meta.fullPath + "/" + m.path),
												meta: {id: m.id, title: m.title, fullPath: r.meta.fullPath + "/" + m.path}
											}
											r.children.push(menu)
										}
									})
									if (r.children) convertTree(r.children)
								})
							}
							convertTree(menuRouters)
							menuRouters.unshift({
								path: "/index",
								component: layout,
								meta: {title: "首页", fullPath: "/index"},
								children: [
									{
										path: "updateRecord",
										component: _import("/index/updateRecord"),
										meta: {title: "系统更新记录", fullPath: "/index/updateRecord"},
									},
									{
										path: "modifyPwd",
										component: _import("/index/modifyPwd"),
										meta: {title: "修改密码", fullPath: "/index/modifyPwd"},
									},
									{
										path: "loginLog",
										component: _import("/index/loginLog"),
										meta: {title: "登录日志", fullPath: "/index/loginLog"},
									}
								]
							})
							store.dispatch("setRouters", menuRouters)
							router.addRoutes(menuRouters)
							router.addRoutes([{path: "*", redirect: "/index/updateRecord"}])
							next({...to, replace: true})
						})
					}
				} else {
					next("/login")
				}
			} else {
				next("/login")
			}
		}
	})
})
router.beforeEach(function (to, from, next) {
	// to 将要进路的路由 route
	// from 离开的路由 route
	// next 进入下一个路由，不调用则不会进入下一个路由
	next()
})
/* 全局解析守卫 */
router.beforeResolve((to, from, next) => {
	next()
})

// return false
router.afterEach((to, from) => {
	NProgress.done() // 结束Progress
	if (to.meta && to.meta.title) {
		document.title = to.meta.title + " | " + store.state.sys.title
	} else {
		document.title = store.state.sys.title
	}
})

export default router
